﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RecruitingApplication.Data_Model;
using System.IO;
using System.Web.UI;
using RecruitingApplication.Admin;
using System.Reflection;

namespace RecruitingApplication
{
    public class BasePage : System.Web.UI.Page
    {
        //Put all WebMethods in this class!

        /// <summary>
        /// Changes application w/ appID from old state to new state w/ ID stateID
        /// </summary>
        /// <param name="appID"></param>
        /// <param name="stateID"></param>
        [System.Web.Services.WebMethod()]
        public static void ChangeApplicationState(int appID, int stateID)
        {
            //set the new state id for the application, updates stale date as well
            DataAccessLayer.OpenDB();
            DataAccessLayer.SetApplicationStateID(appID, stateID);
            DataAccessLayer.CloseDB();
        }

        /// <summary>
        /// Changes application w/ appID from old position to new position w/ ID posID
        /// </summary>
        /// <param name="appID"></param>
        /// <param name="stateID"></param>
        [System.Web.Services.WebMethod()]
        public static void ChangeApplicationPosition(int appID, int posID)
        {
            //set the new state id for the application, updates stale date as well
            DataAccessLayer.OpenDB();
            DataAccessLayer.SetApplicationPositionID(appID, posID);
            DataAccessLayer.CloseDB();
        }

        // Get all Candidates, Positions, and States
        // This is for the Add Application Dialog box
        [System.Web.Services.WebMethod()]
        public static string GetAddAppDlgData_Json() 
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetAddAppDlgData_Json();
            DataAccessLayer.CloseDB();
            return json;
        }

        // Get all Candidates, Positions, and States
        // This is for the Add Application Dialog box
        [System.Web.Services.WebMethod()]
        public static string GetSearchData_Json()
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetSearchData_Json();
            DataAccessLayer.CloseDB();
            return json;
        }

        /// <summary>
        /// Retrieves all 'HeardAbouts' from the database, and returns them
        /// in a json formatted string
        /// </summary>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static string GetAllHeardAbouts_Json()
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetAllHeardAbouts_Json();
            DataAccessLayer.CloseDB();

            return json;
        }

        // Check to see if a candidate is already in the database
        [System.Web.Services.WebMethod()]
        public static bool CheckCandidate(string name)
        {
            bool check = false;
            DataAccessLayer.OpenDB();
            check = DataAccessLayer.CheckCandidate(name);
            DataAccessLayer.CloseDB();
            return check;
        }

        // Retrieves all Candidates from the database matching a name
        // Returns candidates name, email, and phone (for Check Candidate window)
        [System.Web.Services.WebMethod()]
        public static string CheckCandidates_Json(string match)
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.CheckCandidatesMatching_Json(match);
            DataAccessLayer.CloseDB();

            return json;
        }

        /// <summary>
        /// Retrieves all Positions from the database, and returns them
        /// in a json formatted string
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static string GetAllPositions_Json()
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetAllPositions_Json();
            DataAccessLayer.CloseDB();

            return json;
        }

        /// <summary>
        /// Retrieves all Candidates from the database, and returns them
        /// in a json formatted string
        /// </summary>
        /// <param name="match"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static string GetAllCandidates_Json(string match)
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetAllCandidatesMatching_Json(match);
            DataAccessLayer.CloseDB();

            return json;
        }

        [System.Web.Services.WebMethod()]
        public static string GetAllCategories_Json()
        {
            DataAccessLayer.OpenDB();
            string json = DataAccessLayer.GetAllCategories_Json();
            DataAccessLayer.CloseDB();

            return json;
        }

        /// <summary>
        /// Checks if a state can be deleted, then deletes it
        /// </summary>
        /// <param name="id"></param>
        /// <returns>True if state was deleted, otherwise false</returns>
        [System.Web.Services.WebMethod()]
        public static bool DeleteState(int id)
        {
            bool bSuccessful = false;

            DataAccessLayer.OpenDB();

            int count = DataAccessLayer.GetApplicationsCount(id, null);

            if ( count == 0)
            {
                try
                {
                    DataAccessLayer.RemoveState(id);
                    bSuccessful = true;
                }
                catch
                {
                    bSuccessful = false;
                }
            }

            DataAccessLayer.CloseDB();

            return bSuccessful;
        }


        [System.Web.Services.WebMethod()]
        public static bool DeleteHeardAbout(int id)
        {
            bool bSuccessful = false;

            DataAccessLayer.OpenDB();

            int count = DataAccessLayer.GetCandidatesCount(id);

            if (count == 0)
            {
                try
                {
                    DataAccessLayer.RemoveHeardAbout(id);
                    bSuccessful = true;
                }
                catch
                {
                    bSuccessful = false;
                }
            }

            DataAccessLayer.CloseDB();

            return bSuccessful;
        }

        /// <summary>
        /// Creates a candidate in the database
        /// </summary>
        [System.Web.Services.WebMethod()]
       // public static void CreateCandidate(string name, string email, string phone,
        public static int CreateCandidate(string name, string email, string phone,
            int heardAboutID, string notes)
        {
            int returnID = -1;
            DataAccessLayer.OpenDB();
            returnID = DataAccessLayer.AddCandidate(name, email, phone, heardAboutID, notes);
            DataAccessLayer.CloseDB();
            return returnID;
        }

        [System.Web.Services.WebMethod()]
        public static void SetCandidateName(int candID, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception("Name can't be empty");
            }

            //set the new state id for the application, updates stale date as well
            DataAccessLayer.OpenDB();

            DataAccessLayer.SetCandidateName(candID, name);
            DataAccessLayer.CloseDB();
        }

        /// Add an Application to a candidate
        [System.Web.Services.WebMethod()]
        public static void AddApplication(int candID, int positionID, int stateID) 
        {
            DataAccessLayer.OpenDB();
            //TODO: set what user is adding this application!!!!
            DataAccessLayer.AddApplication(candID, positionID, stateID, 0);
            DataAccessLayer.CloseDB();
        }

        /// <summary>
        /// Adds a new state, returns the id of the state
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static int AddState(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception("Name can't be empty");
            }

            int id = -1;

            DataAccessLayer.OpenDB();
            id = DataAccessLayer.AddState(name);
            DataAccessLayer.CloseDB();

            return id;
        }

        [System.Web.Services.WebMethod()]
        public static void EditCandidate(int candID, string name, string email, string phone, int heardAboutID, string notes)
        {   
            DataAccessLayer.OpenDB();
            DataAccessLayer.EditCandidate(candID, name, email, phone, heardAboutID, notes);
            DataAccessLayer.CloseDB();
        }

        [System.Web.Services.WebMethod()]
        public static int AddHeardAbout(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception("Name can't be empty");
            }

            int id = -1;

            DataAccessLayer.OpenDB();
            //TODO: allow user to enter description
            id = DataAccessLayer.AddHeardAbout(name, "");
            DataAccessLayer.CloseDB();

            return id;
        }

        /// <summary>
        /// Sets state orders in the db
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static void SetOrderIndex(int[] ids)
        {
            Dictionary<int, int> dict = new Dictionary<int, int>();

            int index = 0;
            foreach (int id in ids)
            {
                dict.Add(id, index++);
            }

            DataAccessLayer.OpenDB();
            DataAccessLayer.SetStateOrder(dict);
            DataAccessLayer.CloseDB();
        }

        /// <summary>
        /// Checks if a position can be deleted, then deletes it
        /// </summary>
        /// <param name="id"></param>
        /// <returns>True if position was deleted, otherwise false</returns>
        [System.Web.Services.WebMethod()]
        public static bool DeletePosition(int id)
        {
            bool bSuccessful = false;

            DataAccessLayer.OpenDB();

            int count = DataAccessLayer.GetApplicationsCount(null, id);

            if (count == 0)
            {
                try
                {
                    DataAccessLayer.RemovePosition(id);
                    bSuccessful = true;
                }
                catch
                {
                    bSuccessful = false;
                }
            }

            DataAccessLayer.CloseDB();

            return bSuccessful;
        }

        /// <summary>
        /// Adds a new position, returns the id of the position
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod()]
        public static int AddPosition(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new Exception("Name can't be empty");
            }

            int id = -1;

            DataAccessLayer.OpenDB();
            id = DataAccessLayer.AddPosition(name);
            DataAccessLayer.CloseDB();

            return id;
        }   
    }
}